package com.zp.framework.services.test;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.psychological.ResultBaseOrganizationVo;
import com.zp.bean.psychological.ResultOrganizationVo;
import com.zp.bean.user.ResultBaseUserVo;
import com.zp.domain.ModuleOrganization;
import com.zp.domain.Organization;
import com.zp.domain.PersonOrganization;
import com.zp.util.Constant;

/**
 * test organization services function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/3/5
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunOrganizationServices {

	private static final Log logger = LogFactory
			.getLog(RunOrganizationServices.class);
	private static final String URI = "http://localhost:8082/ZP/rest/";
	private WebResource service;

	@Before
	public void init() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		service = client.resource(URI);
		logger.info("*********** testing to initial services client ****************");
	}

	@After
	public void destory() {
		service = null;
		logger.info("*********** testing to destory services instance ******************");
	}

	/**@Test**/
	public void testAddType() {
		try {
			Organization o = new Organization();
			o.setAddress("四川省成都市武侯区科华路");
			o.setCharacter("非营利性组织");
			o.setCityCode("028");
			o.setImage(null);
			o.setIntroduce("电子科技大学青少年行动起始于1994年3月....");
			o.setManage("电子科技大学教育培训");
			o.setMembers(150);
			o.setName("电子科技大学教育培训组织");
			o.setPhone("028-88888888");
			o.setSummary("电子科技大学青少年行动起始于");
			o.setWeibo("http://weibo.com/schxyx");
			ClientResponse response = service.path("organization/addO")
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class, o);
			ResultCommonVo result = response.getEntity(ResultCommonVo.class);
			Assert.assertEquals(Constant.OK, result.getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}

	}

	/**@Test**/
	public void testAssignModule() {
		try {
			ModuleOrganization mo = new ModuleOrganization();
			mo.setModuleId("4028810844ca14300144ca314b780003");
			mo.setOrganizationId("4028810844cb26d60144cb44f4e1000c");
			ClientResponse response = service.path("organization/assignModule")
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class, mo);
			ResultCommonVo result = response.getEntity(ResultCommonVo.class);
			Assert.assertEquals(Constant.OK, result.getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}

	}

	/** @Test **/
	public void testGetOrganizationById() {
		ClientResponse response = service
				.path("organization/getOrganization")
				.queryParam("organizationId",
						"402880e44492d4ac014492d533850001")
				.post(ClientResponse.class);
		ResultOrganizationVo result = response
				.getEntity(ResultOrganizationVo.class);
		Assert.assertNull(result.getVo());
	}

	/** @Test **/
	public void testGetOrganizationWithNameOrCity() {
		ClientResponse response = service.path("organization/getOrganizations")
				.queryParam("pageNo", "1").queryParam("pageSize", "2").queryParam("moduleCode", "mo01")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.post(ClientResponse.class);
		ResultOrganizationVo result = response
				.getEntity(ResultOrganizationVo.class);
		Assert.assertEquals(6, result.getTotalCount());
	}

	@Test
	public void testGetBaseOrganizationWithNameOrCity() {
		ClientResponse response = service.path("organization/getBaseOrans")
				.queryParam("pageNo", "1").queryParam("pageSize", "2").queryParam("moduleCode", "mo02").queryParam("value", "028")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.post(ClientResponse.class);
		ResultBaseOrganizationVo result = response
				.getEntity(ResultBaseOrganizationVo.class);
		Assert.assertEquals(6, result.getTotalCount());
	}

	/** @Test **/
	public void testGetUserFromOrganization() {
		ClientResponse response = service
				.path("organization/getUsers")
				.queryParam("pageNo", "1")
				.queryParam("pageSize", "2")
				.queryParam("organizationId",
						"402880e44492d4ac014492d533850001")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.post(ClientResponse.class);
		ResultBaseUserVo result = response.getEntity(ResultBaseUserVo.class);
		Assert.assertEquals(10, result.getTotalCount());
	}

	@Test
	public void testAddUser() {
		try {
			PersonOrganization po = new PersonOrganization();
			po.setOrganizationId("4028810844cb26d60144cb44f4e1000c");
			po.setUserId("4028810844c8ebd90144c95239ec0059");
			ClientResponse response = service.path("organization/addUser")
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class, po);
			ResultCommonVo result = response.getEntity(ResultCommonVo.class);
			Assert.assertEquals(Constant.OK, result.getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}
	}

}
